package org.mybatis.generator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import com.demo.constans.ApiConstans;

/**
 * @project: 新一代结算系统
 * @description: 实体类添加注释。
 * @version 1.0.0
 * @errorcode
 *            错误码: 错误描述
 * @author
 *         <li>2019-05-15 chengls13783@hundsun.com Create 1.0
 * @copyright ©2017-2019 中央结算公司，版权所有。
 */
public class MyCommentGenerator extends DefaultCommentGenerator
{
    private Properties properties;
    private boolean    suppressDate;
    private boolean    suppressAllComments;
    private String     currentDateStr;
    
    public MyCommentGenerator()
    {
        super();
        properties = new Properties();
        suppressDate = false;
        suppressAllComments = false;
        currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }
    
    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable)
    {
        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * @project:" + ApiConstans.HEADER_CLASS.getSysName());
        innerClass.addJavaDocLine(" * @description:" + introspectedTable.getFullyQualifiedTable().getRemarks());
        innerClass.addJavaDocLine(" * @version 1.0.0");
        innerClass.addJavaDocLine(" * @errorcode");
        innerClass.addJavaDocLine(" *            错误码: 错误描述");
        innerClass.addJavaDocLine(" * @author");
        innerClass
            .addJavaDocLine(" *         <li>2019-01-23 " + ApiConstans.HEADER_CLASS.getUserName() + " Create 1.0");
        // innerClass.addJavaDocLine(" * @copyright ©2017-2019 中央结算公司，版权所有。 ");
        innerClass.addJavaDocLine(" */");
        return;
    }
    
    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNoDelete)
    {
        return;
    }
    
    @Override
    public void addConfigurationProperties(Properties arg0)
    {
        this.properties.putAll(arg0);
        
        suppressDate = Boolean.valueOf(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
        
        suppressAllComments = Boolean
            .valueOf(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
    }
    
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable)
    {
        StringBuilder sb = new StringBuilder();
        
        field.addJavaDocLine("");
        sb.append("/**");
        sb.append(field.getName());
        sb.append(" */");
        field.addJavaDocLine(sb.toString().replace("\n", " "));
    }
    
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn)
    {
        if (suppressAllComments) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        
        field.addJavaDocLine("");
        sb.append("/**");
        sb.append(introspectedColumn.getRemarks());
        sb.append(" */");
        field.addJavaDocLine(sb.toString().replace("\n", " "));
    }
    
    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable)
    {
        StringBuilder sb = new StringBuilder();
        method.addJavaDocLine("");
        method.addJavaDocLine("/**");
        
        if (method.isConstructor()) {
            sb.append(" * 构造方法 ");
            method.addJavaDocLine(sb.toString().replace("\n", " "));
            sb.setLength(0);
        }
        // 参数注释
        List<Parameter> parameters = method.getParameters();
        if (parameters != null) {
            for (Parameter parameter : parameters) {
                sb.append(" * @param ").append(parameter.getName()).append(" ").append(parameter.getName());
                method.addJavaDocLine(sb.toString().replace("\n", " "));
                sb.setLength(0);
            }
            
        }
        
        // 返回值注释
        FullyQualifiedJavaType returnType = method.getReturnType();
        if (returnType != null) {
            String returnName = returnType.getShortName();
            if (!"void".equals(returnName)) {
                sb.append(" * @return ").append(returnName);
                method.addJavaDocLine(sb.toString().replace("\n", " "));
                sb.setLength(0);
            }
        }
        
        method.addJavaDocLine(" */");
        return;
        
    }
    
    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn)
    {
        if (suppressAllComments) {
            return;
        }
        method.addJavaDocLine("");
        method.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * @return ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        method.addJavaDocLine(" */");
    }
    
    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit)
    {
        // compilationUnit.addFileCommentLine("/**");
        // compilationUnit.addFileCommentLine(" * @project:" +
        // ApiConstans.HEADER_CLASS.getSysName());
        // compilationUnit.addFileCommentLine(
        // " * @description: 自动生成mapper" /*
        // * +
        // * introspectedTable.getFullyQualifiedTable().getRemarks()
        // */);
        // compilationUnit.addFileCommentLine(" * @version 1.0.0");
        // compilationUnit.addFileCommentLine(" * @errorcode");
        // compilationUnit.addFileCommentLine(" * 错误码: 错误描述");
        // compilationUnit.addFileCommentLine(" * @author");
        // compilationUnit
        // .addFileCommentLine(" * <li>2019-01-23 " + ApiConstans.HEADER_CLASS.getUserName() + "
        // Create 1.0");
        // compilationUnit.addFileCommentLine(" * @copyright ©2017-2019 中央结算公司，版权所有。 ");
        // compilationUnit.addFileCommentLine(" */");
        return;
    }
    
    @Override
    public void addRootComment(XmlElement arg0)
    {
        return;
    }
    
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn)
    {
        if (suppressAllComments) {
            return;
        }
        method.addJavaDocLine("");
        method.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * @param ");
        sb.append(introspectedColumn.getJavaProperty()).append(" ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString().replace("\n", " "));
        method.addJavaDocLine(" */");
    }
    
    protected void addJavaDocTag(JavaElement javaElement, boolean markAsDoNoDelete)
    {
        javaElement.addJavaDocLine(" *");
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append(MergeConstants.NEW_ELEMENT_TAG);
        if (markAsDoNoDelete) {
            sb.append(" do_not_delete_during_merge");
        }
        String s = getDateString();
        if (s != null) {
            sb.append(" ");
            sb.append(s);
        }
        javaElement.addJavaDocLine(sb.toString());
    }
    
    @Override
    protected String getDateString()
    {
        String result = null;
        if (!suppressDate) {
            result = currentDateStr;
        }
        return result;
    }
    
    public void addClassComment(Interface interfaze, IntrospectedTable introspectedTable)
    {
        interfaze.addJavaDocLine("/**");
        interfaze.addJavaDocLine(" * @project:" + ApiConstans.HEADER_CLASS.getSysName());
        interfaze.addJavaDocLine(" * @description:" + introspectedTable.getFullyQualifiedTable().getRemarks());
        interfaze.addJavaDocLine(" * @version 1.0.0");
        interfaze.addJavaDocLine(" * @errorcode");
        interfaze.addJavaDocLine(" *            错误码: 错误描述");
        interfaze.addJavaDocLine(" * @author");
        interfaze.addJavaDocLine(" *         <li>2019-01-23 " + ApiConstans.HEADER_CLASS.getUserName() + " Create 1.0");
        // interfaze.addJavaDocLine(" * @copyright ©2017-2019 中央结算公司，版权所有。 ");
        interfaze.addJavaDocLine(" */");
        return;
        
    }
    
}
